Skip to content

Commit 4de3ffb

Browse files
Nathan ReidGuillaume Chau
Nathan Reid
authored and
Guillaume Chau
committed
implement lazy-loading of components; fixes dynamic imports (#215)
* implement lazy-loading of components; fixes dynamic imports * Fix merge
1 parent 9388051 commit 4de3ffb

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

packages/vue-component/plugin/vue-compiler.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,37 @@ VueComponentCompiler = class VueCompo extends CachingCompiler {
151151

152152
//console.log(`js hash: ${jsHash}`);
153153

154+
// Lazy load files from NPM packages or imports directories
155+
const isLazy = !!inputFilePath.match(/(^|\/)(node_modules|imports)\//);
156+
157+
// Style
158+
let css = '';
159+
let cssHash = '';
160+
if (compileResult.styles.length !== 0) {
161+
for (let style of compileResult.styles) {
162+
css += style.css;
163+
}
164+
165+
cssHash = Hash(css);
166+
167+
//console.log(`css hash: ${cssHash}`);
168+
if (!isDev && isLazy) {
169+
// Wrap CSS in Meteor's lazy CSS loader
170+
css = `
171+
const modules = require('meteor/modules');
172+
modules.addStyles(${JSON.stringify(css)});
173+
`;
174+
}
175+
}
176+
154177
const { js, templateHash } = generateJs(vueId, inputFile, compileResult)
155178

156-
let outputFilePath = inputFile.getPathInPackage();
179+
let outputFilePath = inputFilePath;
157180
// Meteor will error when loading .vue files on the server unless they are postfixed with .js
158181
if (inputFile.getArch().indexOf('os') === 0 && inputFilePath.indexOf('node_modules') !== -1) {
159182
outputFilePath += '.js';
160183
}
161-
184+
162185
// Including the source maps for .vue files from node_modules breaks source mapping.
163186
const sourceMap = inputFilePath.indexOf('node_modules') === -1
164187
? compileResult.map
@@ -167,27 +190,17 @@ VueComponentCompiler = class VueCompo extends CachingCompiler {
167190
// Add JS Source file
168191
inputFile.addJavaScript({
169192
path: outputFilePath,
170-
data: js,
193+
data: isLazy && !isDev ? css + js : js,
171194
sourceMap: sourceMap,
172-
lazy: false,
195+
lazy: isLazy,
173196
});
174197

175-
// Style
176-
let css = '';
177-
let cssHash = '';
178-
if (compileResult.styles.length !== 0) {
179-
for (let style of compileResult.styles) {
180-
css += style.css;
181-
}
182-
183-
cssHash = Hash(css);
184-
185-
//console.log(`css hash: ${cssHash}`);
186-
198+
if (css) {
187199
if (isDev) {
188200
// Add style to client first-connection style list
189201
global._dev_server.__addStyle({ hash: vueId, css, path: inputFilePath }, false);
190-
} else {
202+
} else if (!isLazy) {
203+
// In order to avoid lazy-loading errors in --production mode, addStylesheet must come after addJavaScript
191204
this.addStylesheet(inputFile, {
192205
data: css
193206
});

0 commit comments

Comments
 (0)