-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(build): add support for assets array (#2570)
- Loading branch information
1 parent
1b46c11
commit de3c275
Showing
10 changed files
with
87 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as glob from 'glob'; | ||
import * as denodeify from 'denodeify'; | ||
|
||
const globPromise = <any>denodeify(glob); | ||
const statPromise = <any>denodeify(fs.stat); | ||
|
||
export interface GlobCopyWebpackPluginOptions { | ||
patterns: string[]; | ||
globOptions: any; | ||
} | ||
|
||
export class GlobCopyWebpackPlugin { | ||
constructor(private options: GlobCopyWebpackPluginOptions) { } | ||
|
||
apply(compiler: any): void { | ||
let { patterns, globOptions } = this.options; | ||
let context = globOptions.cwd || compiler.options.context; | ||
|
||
// convert dir patterns to globs | ||
patterns = patterns.map(pattern => fs.statSync(path.resolve(context, pattern)).isDirectory() | ||
? pattern += '/**/*' | ||
: pattern | ||
); | ||
|
||
// force nodir option, since we can't add dirs to assets | ||
globOptions.nodir = true; | ||
|
||
compiler.plugin('emit', (compilation: any, cb: any) => { | ||
let globs = patterns.map(pattern => globPromise(pattern, globOptions)); | ||
|
||
let addAsset = (relPath: string) => compilation.assets[relPath] | ||
// don't re-add to assets | ||
? Promise.resolve() | ||
: statPromise(path.resolve(context, relPath)) | ||
.then((stat: any) => compilation.assets[relPath] = { | ||
size: () => stat.size, | ||
source: () => fs.readFileSync(path.resolve(context, relPath)) | ||
}); | ||
|
||
Promise.all(globs) | ||
// flatten results | ||
.then(globResults => [].concat.apply([], globResults)) | ||
// add each file to compilation assets | ||
.then(relPaths => relPaths.forEach((relPath: string) => addAsset(relPath))) | ||
.catch((err) => compilation.errors.push(err)) | ||
.then(cb); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
import {writeFile, expectFileToExist, expectFileToMatch} from '../../utils/fs'; | ||
import {ng} from '../../utils/process'; | ||
import {updateJsonFile} from '../../utils/project'; | ||
import {expectToFail} from '../../utils/utils'; | ||
|
||
|
||
export default function() { | ||
return writeFile('src/assets/.file', '') | ||
.then(() => writeFile('src/assets/test.abc', 'hello world')) | ||
.then(() => ng('build')) | ||
.then(() => expectFileToExist('dist/favicon.ico')) | ||
.then(() => expectFileToExist('dist/assets/.file')) | ||
.then(() => expectFileToMatch('dist/assets/test.abc', 'hello world')) | ||
.then(() => expectToFail(() => expectFileToExist('dist/assets/.gitkeep'))); | ||
.then(() => expectToFail(() => expectFileToExist('dist/assets/.gitkeep'))) | ||
// doesn't break beta.16 projects | ||
.then(() => updateJsonFile('angular-cli.json', configJson => { | ||
const app = configJson['apps'][0]; | ||
app['assets'] = 'assets'; | ||
})) | ||
.then(() => expectFileToExist('dist/assets/.file')) | ||
.then(() => expectFileToMatch('dist/assets/test.abc', 'hello world')); | ||
} |