mirror of
https://github.com/kevin-DL/sapper.git
synced 2026-01-12 03:05:12 +00:00
use shimport
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"html-minifier": "^3.5.16",
|
||||
"shimport": "^0.0.4",
|
||||
"shimport": "^0.0.5",
|
||||
"source-map-support": "^0.5.6",
|
||||
"tslib": "^1.9.1"
|
||||
},
|
||||
|
||||
@@ -8,6 +8,7 @@ import { create_compilers, create_main_manifests, create_routes, create_servicew
|
||||
import { Compilers, Compiler } from '../core/create_compilers';
|
||||
import * as events from './interfaces';
|
||||
import validate_bundler from '../cli/utils/validate_bundler';
|
||||
import { copy_shimport } from './utils/copy_shimport';
|
||||
|
||||
export function build(opts: {}) {
|
||||
const emitter = new EventEmitter();
|
||||
@@ -34,8 +35,9 @@ async function execute(emitter: EventEmitter, {
|
||||
rollup = 'rollup',
|
||||
routes = 'routes'
|
||||
} = {}) {
|
||||
mkdirp.sync(dest);
|
||||
mkdirp.sync(`${dest}/client`);
|
||||
rimraf.sync(path.join(dest, '**/*'));
|
||||
copy_shimport(dest);
|
||||
|
||||
// minify app/template.html
|
||||
// TODO compile this to a function? could be quicker than str.replace(...).replace(...).replace(...)
|
||||
@@ -66,6 +68,7 @@ async function execute(emitter: EventEmitter, {
|
||||
|
||||
fs.writeFileSync(path.join(dest, 'build.json'), JSON.stringify({
|
||||
bundler,
|
||||
shimport: bundler === 'rollup' && require('shimport/package.json').version,
|
||||
assets: client_result.assetsByChunkName
|
||||
}));
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ import { Compiler, Compilers, CompileResult, CompileError } from '../core/create
|
||||
import Deferred from './utils/Deferred';
|
||||
import * as events from './interfaces';
|
||||
import validate_bundler from '../cli/utils/validate_bundler';
|
||||
import { copy_shimport } from './utils/copy_shimport';
|
||||
|
||||
export function dev(opts) {
|
||||
return new Watcher(opts);
|
||||
@@ -110,7 +111,8 @@ class Watcher extends EventEmitter {
|
||||
|
||||
const { dest } = this.dirs;
|
||||
rimraf.sync(dest);
|
||||
mkdirp.sync(dest);
|
||||
mkdirp.sync(`${dest}/client`);
|
||||
if (this.bundler === 'rollup') copy_shimport(dest);
|
||||
|
||||
const dev_port = await ports.find(10000);
|
||||
|
||||
@@ -271,6 +273,7 @@ class Watcher extends EventEmitter {
|
||||
handle_result: (result: CompileResult) => {
|
||||
fs.writeFileSync(path.join(dest, 'build.json'), JSON.stringify({
|
||||
bundler: this.bundler,
|
||||
shimport: this.bundler === 'rollup' && require('shimport/package.json').version,
|
||||
assets: result.assetsByChunkName
|
||||
}, null, ' '));
|
||||
this.deferreds.client.fulfil();
|
||||
|
||||
9
src/api/utils/copy_shimport.ts
Normal file
9
src/api/utils/copy_shimport.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import * as fs from 'fs';
|
||||
|
||||
export function copy_shimport(dest: string) {
|
||||
const shimport_version = require('shimport/package.json').version;
|
||||
fs.writeFileSync(
|
||||
`${dest}/client/shimport@${shimport_version}.js`,
|
||||
fs.readFileSync(require.resolve('shimport/index.dev.js'))
|
||||
);
|
||||
}
|
||||
@@ -305,6 +305,7 @@ function get_page_handler(
|
||||
function handle_page(page: Page, req: Req, res: ServerResponse, status = 200, error: Error | string = null) {
|
||||
const build_info: {
|
||||
bundler: 'rollup' | 'webpack',
|
||||
shimport: string | null,
|
||||
assets: Record<string, string | string[]>
|
||||
} = get_build_info();
|
||||
|
||||
@@ -357,7 +358,6 @@ function get_page_handler(
|
||||
);
|
||||
|
||||
if (include_cookies) {
|
||||
const cookies: Record<string, string> = {};
|
||||
if (!opts.headers) opts.headers = {};
|
||||
|
||||
const str = []
|
||||
@@ -488,7 +488,7 @@ function get_page_handler(
|
||||
const main = `${req.baseUrl}/client/${file}`;
|
||||
|
||||
const script = build_info.bundler === 'rollup'
|
||||
? `<script>try{new Function("import('${main}')")();}catch(e){var s=document.createElement("script");s.src="client/shimport.js";s.setAttribute('data-main',"${main}");document.head.appendChild(s)}</script>`
|
||||
? `<script>try{new Function("import('${main}')")();}catch(e){var s=document.createElement("script");s.src="${req.baseUrl}/client/shimport@${build_info.shimport}.js";s.setAttribute("data-main","${main}");document.head.appendChild(s);}</script>`
|
||||
: `<script src="${main}"></script>`;
|
||||
|
||||
let inline_script = `__SAPPER__={${[
|
||||
|
||||
Reference in New Issue
Block a user