Skip to content

Commit cf2f586

Browse files
committed
feat: inject css on demand
BREAKING CHANGE: option injectCss is now turned on by default
1 parent 177b008 commit cf2f586

3 files changed

Lines changed: 56 additions & 13 deletions

File tree

spec/bundler.spec.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ function mockTrace(unit) {
99
const moduleId = unit.moduleId;
1010
const shim = unit.shim;
1111

12-
if (unit.moduleId === 'ext:css') {
12+
if (unit.moduleId === 'ext:css' || unit.path.endsWith('.css')) {
1313
// don't mock ext:css trace
14+
// don't mock css file
1415
return trace(unit);
1516
}
1617

@@ -758,18 +759,18 @@ test('Bundler traces files, split bundles, continuously update bundles in watch
758759
.then(t.end);
759760
});
760761

761-
test('Bundler supports inject css', t => {
762+
test('Bundler supports inject css by default', t => {
762763
const fakeFs = {
763764
'node_modules/dumber-module-loader/dist/index.js': 'dumber-module-loader',
764765
'node_modules/dumber/package.json': JSON.stringify({name: 'dumber', main: './dist/index'}),
765766
'node_modules/dumber/dist/inject-css.js': '',
766767
};
767768
const bundler = createBundler(fakeFs, {
768-
injectCss: true
769769
});
770770

771771
Promise.resolve()
772-
.then(() => bundler.capture({path: 'src/app.js', contents: '', moduleId: 'app'}))
772+
.then(() => bundler.capture({path: 'src/app.js', contents: 'c.css', moduleId: 'app'}))
773+
.then(() => bundler.capture({path: 'src/c.css', contents: 'lorem', moduleId: 'c.css'}))
773774
.then(() => bundler.resolve())
774775
.then(() => bundler.bundle())
775776
.then(
@@ -779,7 +780,8 @@ test('Bundler supports inject css', t => {
779780
files: [
780781
{contents: 'dumber-module-loader;'},
781782
{contents: 'define.switchToUserSpace();'},
782-
{path: 'src/app.js', contents: "define('app',[],1);", sourceMap: undefined},
783+
{path: 'src/app.js', contents: "define('app',[\"c.css\"],1);", sourceMap: undefined},
784+
{path: 'src/c.css', contents: "define('text!c.css',function(){return \"lorem\";});", sourceMap: undefined},
783785
{path: '__stub__/ext-css.js', contents: "define('ext:css',['dumber/dist/inject-css'],function(m){return m;});", sourceMap: undefined},
784786
{contents: 'define.switchToPackageSpace();'},
785787
{path: 'node_modules/dumber/dist/inject-css.js', contents: "define('dumber/dist/inject-css',[],1);", sourceMap: undefined},
@@ -798,6 +800,44 @@ test('Bundler supports inject css', t => {
798800
.then(t.end);
799801
});
800802

803+
test('Bundler can optionally turn off inject css', t => {
804+
const fakeFs = {
805+
'node_modules/dumber-module-loader/dist/index.js': 'dumber-module-loader',
806+
'node_modules/dumber/package.json': JSON.stringify({name: 'dumber', main: './dist/index'}),
807+
'node_modules/dumber/dist/inject-css.js': '',
808+
};
809+
const bundler = createBundler(fakeFs, {
810+
injectCss: false
811+
});
812+
813+
Promise.resolve()
814+
.then(() => bundler.capture({path: 'src/app.js', contents: 'c.css', moduleId: 'app'}))
815+
.then(() => bundler.capture({path: 'src/c.css', contents: 'lorem', moduleId: 'c.css'}))
816+
.then(() => bundler.resolve())
817+
.then(() => bundler.bundle())
818+
.then(
819+
bundleMap => {
820+
t.deepEqual(bundleMap, {
821+
'entry-bundle': {
822+
files: [
823+
{contents: 'dumber-module-loader;'},
824+
{contents: 'define.switchToUserSpace();'},
825+
{path: 'src/app.js', contents: "define('app',[\"c.css\"],1);", sourceMap: undefined},
826+
{path: 'src/c.css', contents: "define('text!c.css',function(){return \"lorem\";});", sourceMap: undefined}
827+
],
828+
config: {
829+
baseUrl: '/dist',
830+
bundles: {},
831+
paths: {}
832+
}
833+
}
834+
})
835+
},
836+
err => t.fail(err.stack)
837+
)
838+
.then(t.end);
839+
});
840+
801841
test('Bundler traces files with paths mapping', t => {
802842
const fakeFs = {
803843
'node_modules/dumber-module-loader/dist/index.js': 'dumber-module-loader',

src/index.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ export default class Bundler {
4040
this._cache = cache;
4141
}
4242

43-
// turn on injection of css (inject onto html head)
44-
if (opts.injectCss || opts.injectCSS) this._injectCss = true;
43+
if (opts.hasOwnProperty('injectCss') && !opts.injectCss) {
44+
this._injectCss = false;
45+
} else {
46+
// by default turn on injection of css (inject onto html head)
47+
this._injectCss = true;
48+
}
4549

4650
let _paths = {};
4751
if (opts.paths) {
@@ -150,8 +154,6 @@ export default class Bundler {
150154

151155
// mark todo. beware we didn't check whether the id is in _moduleId_done.
152156
// they will be checked during resolve phase.
153-
154-
// console.log('_capture ' + tracedUnit.moduleId + ' deps ' + tracedUnit.deps);
155157
tracedUnit.deps.forEach(d => this._moduleIds_todo.add(d));
156158

157159
const bundle = this.bundleOf(tracedUnit);
@@ -173,7 +175,6 @@ export default class Bundler {
173175
this._dependencies.forEach(pkg => {
174176
p = p.then(() => this.packageReaderFor(pkg)).then(reader => {
175177
if (!pkg.lazyMain) {
176-
// console.log('to readMain for ' + pkg.name);
177178
return reader.readMain()
178179
.then(unit => this.capture(unit))
179180
.then(tracedUnit => {
@@ -243,8 +244,7 @@ export default class Bundler {
243244

244245
resolve() {
245246
let todo = [];
246-
return this._supportInjectCssIfNeeded()
247-
.then(() => this._resolvePrependsAndAppends())
247+
return this._resolvePrependsAndAppends()
248248
.then(() => this._resolveExplicitDepsIfNeeded())
249249
.then(() => {
250250
const consults = [];
@@ -253,6 +253,9 @@ export default class Bundler {
253253

254254
rawTodo.forEach(id => {
255255
const parsedId = parse(mapId(id, this._paths));
256+
if (!parsedId.prefix && parsedId.ext === '.css' && !this._isInjectCssTurnedOn) {
257+
consults.push(this._supportInjectCssIfNeeded());
258+
}
256259
const possibleIds = nodejsIds(parsedId.bareId);
257260
if (possibleIds.some(id => this._moduleId_done.has(id))) return;
258261

src/package-locators/default.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function (packageConfig, mock) {
3939
},
4040
err => {
4141
if (filePath === 'package.json' || filePath === './package.json') {
42-
warn('No package.json found at ' + relativePath);
42+
warn('No package.json found at ' + name + '/' + relativePath);
4343
const mock = `{"name":${JSON.stringify(name)},"main":"index"}`;
4444
info('Fall back to ' + mock);
4545
return {

0 commit comments

Comments
 (0)